package com.wanmait.baohan.controller.logisticsManagement;

import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.*;

import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2020/12/15 17:59
 * @description:
 */
@Controller
@RequestMapping("/management")
public class CarManagementController {

    @Resource
    StaffService staffService;

    @Resource
    TravelApplicationService travelApplicationService;

    @Resource
    TravelPeopleService travelPeopleService;

    @Resource
    BookingCarService bookingCarService;

    @Resource
    VehicleAccountService vehicleAccountService;

    @Resource
    VehicleService vehicleService;

    @Resource
    private CarApprovalService carApprovalService;
    /**
     * lqc
     * @param model
     * @return
     * 获得派车申请表信息
     */

    //跳转到派车申请表处理
    @RequestMapping(value = "/bookingCarApplicationList")
    public void bookingCarApplicationList(Model model){}

     //获取我的派车申请列表
    @RequestMapping("getCarApplicationsList")
    @ResponseBody
    public DataVo<BookingCarApplication> getCarApplicationsList(Integer page, Integer limit, HttpServletRequest request){
        //创建查询条件对象
        BookingCarApplication bookingCarApplication = new BookingCarApplication();

        DataVo<BookingCarApplication> dataVo = new DataVo();
        PageInfo<BookingCarApplication> pageInfo = bookingCarService.selectAllCarApplicationsList(page, limit, bookingCarApplication);
        dataVo.setCount(pageInfo.getTotal());
        dataVo.setData(pageInfo.getList());
        return dataVo;

    }
    /**
     * hxn
     * @param model
     * @return
     * 打开派车申请页面
     */
    @RequestMapping("/bookingCarApplication")
    public String bookingHotelApplication(Integer id, Model model){
        model.addAttribute("applicationBasicInfo", travelApplicationService.findById(id));
        model.addAttribute("applicationMembers",travelPeopleService.findAllAllpicationStaffs(id));
        return "/management/bookingCarApplication";
    }

    /**
     * hxn
     * @param id
     * @param request
     * @return
     * 通过项目类型获取所有可以申请派车的项目
     */
    @RequestMapping("/getApplicationsByProjectType")
    @ResponseBody
    public List getApplicationsByProjectType(Integer id, HttpServletRequest request){
        List applications = null;
        Staff staff = (Staff) request.getSession().getAttribute("user");
        Integer userId = staff.getId();
        switch (id){
            case 1:
                applications = travelApplicationService.findAllApplicationCanBooking(userId);
                break;
            case 2:
                //applications = ;
        }
        return applications;
    }

    /**
     * hxn
     * @param request
     * @return
     * 获取当前全部可以申请派车的项目
     */
    @RequestMapping("/getApplicationsCanBookCar")
    @ResponseBody
    public DataVo<TravelApplication> getApplicationsCanBookCar(HttpServletRequest request){
        DataVo<TravelApplication> dataVo = new DataVo<>();
        List applications = null;
        Staff staff = (Staff) request.getSession().getAttribute("user");
        Integer userId = staff.getId();
        applications = travelApplicationService.findAllApplicationCanBooking(userId);
        dataVo.setData(applications);
        dataVo.setCount((long) applications.size());
        return dataVo;
    }

    /**
     * hxn
     * @return
     * 打开可申请派车的项目列表
     */
    @RequestMapping("applicationsCanBookCar")
    public String applicationsCanBookCar(){
        return "management/applicationsCanBookCar";
    }

    /**
     * hxn
     * @param id
     * @return
     * 获取项目中所有成员
     */
    @RequestMapping("getApplicationStaffs")
    @ResponseBody
    public List<TravelPeople> getApplicationStaffs(Integer id){
        return travelPeopleService.findAllAllpicationStaffs(id);
    }

    /**
     * hxn
     * @param bookingCarApplication
     * 提交派车申请
     */
    @RequestMapping("submitCarApplication")
    @ResponseBody
    public void submitCarApplication(BookingCarApplication bookingCarApplication){
        bookingCarService.insert(bookingCarApplication);
    }

    /**
     * hxn
     * @param model
     * @return
     * 打开我的派车申请列表
     */
    @RequestMapping("table-CarApplications")
    public String tebleCarApplciations(Model model){
        return "management/table-CarApplications";
    }

    /**
     * hxn
     * @param page
     * @param limit
     * @param request
     * @return
     * 获取我的派车申请
     */
    @RequestMapping("getMyCarApplications")
    @ResponseBody
    public DataVo<BookingCarApplication> getMyCarApplications(Integer page, Integer limit, HttpServletRequest request){
        //创建查询条件对象
        BookingCarApplication bookingCarApplication = new BookingCarApplication();
        Staff staff = (Staff) request.getSession().getAttribute("user");
        bookingCarApplication.setStaff(staff);


        DataVo<BookingCarApplication> dataVo = new DataVo();
        PageInfo<BookingCarApplication> pageInfo = bookingCarService.selectMyCarApplications(page, limit, bookingCarApplication);
        dataVo.setCount(pageInfo.getTotal());
        dataVo.setData(pageInfo.getList());
        return dataVo;
    }

    /**
     * hxn
     * @param id
     * @param model
     * @return
     * 派车申请详情
     */
    @RequestMapping("getCarApplicationDetails")
    public String getCarApplicationDetails(Integer id, Model model){
        BookingCarApplication bookingCarApplication = bookingCarService.findById(id);
        model.addAttribute("bookingCarApplication", bookingCarApplication);
        model.addAttribute("travelPeoples", travelPeopleService.findAllAllpicationStaffs(bookingCarApplication.getApplicationId()));
        model.addAttribute("approvalLog", carApprovalService.findById(id));
        return "management/carApplicationDetails";
    }

    @RequestMapping("saveCarApprovalForm")
    public void saveCarApprovalForm(Integer id, String value, Integer state){
        CarApproval carApproval = new CarApproval();
        carApproval.setApprovalInfo(value);
        carApproval.setState(state);
        carApproval.setBookingCarApplicationId(id);
        bookingCarService.submitApproval(carApproval);

    }

    /**
     * 林琦淳
     * 打开查看派车台账的控制器
     */
    @RequestMapping("carAccount")
    public  void carAccount(Integer id,Model model){
        System.out.println(id);
        List<VehicleAccount> lsit =vehicleAccountService.findListByid(id);
        List<Vehicle> cars =new ArrayList<>();
        for (VehicleAccount vehicleAccount: lsit) {
            Vehicle car=new Vehicle();
            car.setId(vehicleAccount.getLicensePlateNumber());
            cars.add(car);
        }
        cars=vehicleService.findAllCarByList(cars);
        VehicleAccount account1 =lsit.get(0);
        System.out.println(account1.getAddStaffId());
        Staff staff=staffService.findOneById(account1.getAddStaffId());
        model.addAttribute("account",account1);
        model.addAttribute("cars",cars);
        model.addAttribute("staff",staff);
    }
}
